import Vue from 'vue'
// import wx from '@/common/wechat';

function scanCode(status) {
	// #ifdef H5
	// 调用微信扫一扫
	return new Promise((resolve, reject) => {
		Vue.prototype.$wx.scanQRCode((res) => {
			if (res.errMsg == "scanQRCode:ok") {
				//调用成功
				let result = res.resultStr; //扫码结果
				let regx = /\d{10,18}/;
				let str = '';
				if (result.match(regx) !== null) {
					str = result.match(regx)[0]
				}
				if (str) {
					resolve(str)
				}
			} else {
				reject('微信扫一扫调用失败')
			}
		}, status);
	})
	// #endif
	// #ifdef MP-WEIXIN
	return new Promise((resolve, reject) => {
		uni.scanCode({
			scanType:['barCode','qrCode','datamatrix','pdf417'],
			success: (res) => {
				console.log('条码类型：' + res.scanType);
				console.log('条码内容：' + res.result);
				let result = res.result; //扫码结果
				if(result.indexOf('http') == -1){
					resolve(result)
				}else{
					let regx = /\d{10,}/;
					let str = '';
					if (result.match(regx) !== null) {
						str = result.match(regx)[0]
					}
					if (str) {
						resolve(str)
						// this.form.code = str
						// setTimeout(() => {
						// 	this.search(true)
						// }, 100)
					}
				}
			},
			fail: (e) => {
				console.log(e);
			}
		})
	})
	// #endif 
}
export default {
	scanCode
}
